package com.tengju.user.domain.shared;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Util {

    private Base64Util() {

    }

    /**
     * 解密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String decode(String content, String charset) throws Exception {
        byte[] buffer = decode(content);
        return new String(buffer, charset);
    }

    public static byte[] decode(String content) throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        return decoder.decode(content);
    }

    /**
     * 加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String encode(String content) throws Exception {
        return encode(content.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * 加密
     *
     * @param b
     * @return
     * @throws Exception
     */
    public static String encode(byte[] b) throws Exception {
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(b);
    }
}
